<?php

namespace app\controller\message;

use app\controller\ApiController;

class FcmMessage extends ApiController
{
    /**
     * firebase设置用户设备token
     * @param \app\common\business\sendFcm\FcmMessage $fcmMessage
     * @return \support\Response
     */
    public function fcmToken(\app\common\business\sendFcm\FcmMessage $fcmMessage)
    {
        $userId = request()->post('user_id', 0);
        $fcmToken = request()->post('fcm_token', '');
        $expTime = request()->post('exp_time', 0);
        $deviceId = request()->post('device_id', '');
        $deviceModel = request()->post('device_model', '');
        $platform = request()->post('platform', ''); // 区分设备,android传递,ios不传递

        if (empty($fcmToken)) {
            return self::error('FCM token不能为空');
        }

        $res = $fcmMessage::setFcmToken($userId, $fcmToken, $expTime, $deviceId, $deviceModel, $platform);
        if (empty($res)) {
            return self::error('操作失败');
        }
        return self::success();
    }

    /**
     * 推送消息
     * @param \app\common\business\sendFcm\FcmMessage $fcmMessage
     * @return \support\Response
     */
    public function cloudMessage(\app\common\business\sendFcm\FcmMessage $fcmMessage)
    {
        $tokens = request()->post('token', '');
        $title = request()->post('title', '');
        $content = request()->post('content', '');
        if (empty($tokens)) {
            return self::error('token不能为空');
        }
        if (empty($title) || empty($content)) {
            return self::error('填写发送内容');
        }
        $userFcmTokenModel = new \app\common\model\UserFcmToken();
        $users = $userFcmTokenModel->getFindData(['fcm_token'=>$tokens]);
        if (empty($users)) {
            $users = [
                'id' => 0,
                'fcm_token' => $tokens
            ];
        }
//        $res = $fcmMessage::fcmSend($users, ['id'=>2,'title'=>$title,'content'=>$content]);
        $res = (new \app\common\business\sendFcm\FcmTopic())::toDeviceMsg($users, ['id'=>2,'title'=>$title,'content'=>$content]);

        (new \app\common\model\SendLog())->where(['id'=>$res])->update(['type'=>1,'updated_at' => time()]);

        if (empty($res)) {
            return self::error();
        }
        return self::success($res);
    }

    /**
     * 订阅主题
     * @param \app\common\business\sendFcm\FcmTopic $fcmTopicBus
     * @return \support\Response
     */
    public function addTopic(\app\common\business\sendFcm\FcmTopic $fcmTopicBus)
    {
        $token = request()->post('fcm_token', '');
        if (empty($token)) {
            return self::error('token不能为空');
        }
        $res = $fcmTopicBus::subscribeToTopic($token);
        if (empty($res)) {
            return self::error();
        }
        return self::success($res);
    }

    /**
     * 取消订阅主题
     * @param \app\common\business\sendFcm\FcmTopic $fcmTopicBus
     * @return \support\Response
     */
    public function unDelTopic(\app\common\business\sendFcm\FcmTopic $fcmTopicBus)
    {
        $token = request()->post('fcm_token', '');
        $topic = request()->post('topic', '');
        if (empty($token)) {
            return self::error('token不能为空');
        }
        $res = $fcmTopicBus::unSubscribeToTopic($token, $topic);
        if (empty($res)) {
            return self::error('取消订阅失败');
        }
        return self::success($res);
    }

    /**
     * 查询订阅的主题
     * @param \app\common\business\sendFcm\FcmTopic $fcmTopicBus
     * @return \support\Response
     */
    public function getFindTopics(\app\common\business\sendFcm\FcmTopic $fcmTopicBus)
    {
        $token = request()->post('fcm_token', '');
        if (empty($token)) {
            return self::error('token不能为空');
        }
        $res = $fcmTopicBus::getTopics($token);
        if (empty($res)) {
            return self::error();
        }
        return self::success($res);
    }

    /**
     * 主题发送消息
     * @param \app\common\business\sendFcm\FcmTopic $fcmTopicBus
     * @return \support\Response
     */
    public function sendTopicMsg(\app\common\business\sendFcm\FcmTopic $fcmTopicBus)
    {
        $topic = request()->post('topic', '');
        $title = request()->post('title', '');
        $content = request()->post('content', '');

        if (empty($title) || empty($content)) {
            return self::error('填写发送内容');
        }
        $res = $fcmTopicBus::sendTopicMsg($topic,$title,$content);
        if (empty($res)) {
            return self::error();
        }
        return self::success($res);
    }

    /**
     * 查询token信息
     * @param \app\common\business\sendFcm\FcmTopic $fcmTopicBus
     * @return \support\Response
     * @throws \Kreait\Firebase\Exception\MessagingException
     * @throws \support\exception\BusinessException
     */
    public function valiToken(\app\common\business\sendFcm\FcmTopic $fcmTopicBus)
    {
        $token = request()->post('fcm_token', '');
        if (empty($token)) {
            return self::error('token不能为空');
        }
        $res = $fcmTopicBus::valiTokenBus($token);
        if (empty($res)) {
            return self::error('无效或不可用');
        }
        return self::success($res);
    }

    /**
     * 定时任务,是否有订阅主题
     * @param \app\common\business\sendFcm\FcmTopic $fcmTopicBus
     * @return \support\Response
     * @throws \Kreait\Firebase\Exception\MessagingException
     * @throws \think\db\exception\DbException
     */
    public function checkTokenForTopic(\app\common\business\sendFcm\FcmTopic $fcmTopicBus)
    {
        $res = $fcmTopicBus::checkTokenForTopicBus();
        return self::success($res);
    }

    /**
     * 定时任务,查询toekn是否有效
     * @param \app\common\business\sendFcm\FcmTopic $fcmTopicBus
     * @return \support\Response
     * @throws \think\db\exception\DbException
     */
    public function checkUserToken(\app\common\business\sendFcm\FcmTopic $fcmTopicBus)
    {
        $res = $fcmTopicBus::checkToken();
        return self::success($res);
    }
}
